package com.xiaowu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaowu.mapper.BlindMapper;
import com.xiaowu.mapper.VolunteerMapper;
import com.xiaowu.pojo.*;
import com.xiaowu.service.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class VolunteerServiceImpl implements VolunteerService {
    @Autowired
    private VolunteerMapper vm;

    @Override
    public List<Volunteer> list() {
        return vm.list();
    }
    @Override
    public void delete(Integer id) {
        vm.delete(id);
    }
    @Override
    public void add(Volunteergister volunteer) {
        LocalDateTime registerDate=LocalDateTime.now();

        DateTimeFormatter dfDateTime = DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEE HH:mm:ss", Locale.CHINA);
        String format = registerDate.format(dfDateTime);
        String formats = format.replace("周", "星期");
        volunteer.setRegisterDate(formats);
        volunteer.setLoginStatus("0");
        volunteer.setOnLineStatus("0");
        vm.insert(volunteer);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //设置分页信息
        PageHelper.startPage(page, pageSize);
        //查询数据
        List<Volunteer> list = vm.listByPage();
        Page<Volunteer> p = (Page<Volunteer>) list;
        //封装
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public PageBean pages(Integer page, Integer pageSize, String userId, String onLineStatus, LocalDate begin, LocalDate end) throws ParseException {
        //设置分页信息
        PageHelper.startPage(page, pageSize);
        //查询数据
        //List<Blind> list = bm.listByPages(userId, onLineStatus);
        Page<Volunteer> pageResult = (Page<Volunteer>) vm.listByPages(userId, onLineStatus);
        List<Volunteer> list = pageResult.getResult();
        if(begin !=null && end !=null){
            List<Volunteer> list1 =new ArrayList<>();
            StringToDate st=new StringToDate();
            for (Volunteer volunteer : list) {
                String registerDate = volunteer.getRegisterDate();
                LocalDate localDate = st.StringToDate(registerDate);

                if(localDate.isAfter(begin)&&localDate.isBefore(end)){
                    list1.add(volunteer);

                }

            }
            list=list1;
        }
      /*  Page<Blind> p = (Page<Blind>) list;
        //封装
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());*/
        PageBean pageBean = new PageBean(pageResult.getTotal(), list);
        return pageBean;
    }
}
